/**
 * Project Name:youba
 * Package Name:com.inoneone.youba.mcht.util
 * Date:2017/6/21上午11:39
 * Copyright (c) 2017, www.zbj.com All Rights Reserved
 */
package com.inoneone.youba.mcht.util;

import com.inoneone.youba.mcht.dao.po.Merchant;
import com.inoneone.youba.mcht.enums.Constants;

import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * Description: 通用工具类 </br>
 * Date: 2017/6/21上午11:39 </br>
 *
 * @author 姬远玄
 * @since JDK 1.7
 */
public class CommonUtil {

    /**
     * Description: 获取session</br>
     * Date: 2017-06-21 11:41:26</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7 TODO
    */
    public static Merchant getSession(HttpSession httpSession)
    {
        Merchant merchant = (Merchant)httpSession.getAttribute(Constants.SESSION_KEY);
        return  merchant;
    }

    /**
     * Description: 设置session</br>
     * Date: 2017-06-22 12:01:15</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    public static void setSession(HttpSession httpSession, Merchant merchant)
    {
        httpSession.setAttribute(Constants.SESSION_KEY, merchant);
    }

    /**
     *
     * isNullOrEmpty:(判断字符串是否为空字符串、空白字符串或者null).
     *
     * @author zhoudy
     * @param str
     * @return
     * @since JDK 1.7
     */
    public static boolean isNullOrEmpty(String str) {
        if (str == null || str.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 创建指定数量的随机字符串
     *
     * @param numberFlag
     *            是否是数字
     * @param length
     * @return
     */
    public static String genRandom(boolean numberFlag, int length) {
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

    /**
     * Description: TODO(描述)</br>
     * Date: 2017-06-28 19:05:48</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    public static String genRandomKey()
    {
        return  MD5.encode(UUID.randomUUID().toString());
    }
}
